{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "65\n"
     ]
    }
   ],
   "source": [
    "# Unicode （”Universal Multiple-Octet Coded Character Set”，简称 UCS, 俗称 “unicode“）\n",
    "# UTF-8 万国码， 就是在互联网上使用最广的一种Unicode的实现方式，这是为传输而设计的编码\n",
    "print(chr(65))  # 码点转换成对应字符\n",
    "print(ord('A')) # ordinal， 字符转换成码点\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b1010\n",
      "0b100011\n",
      "0o11\n",
      "0x18a92\n",
      "2\n",
      "35\n"
     ]
    }
   ],
   "source": [
    "# 进制， 0b 开头表示二进制，0o表示八进制，0x表示十六进制，\n",
    "print(bin(10))       # bin -> str\n",
    "print(bin(0x23))    \n",
    "print(oct(0b01001))  # oct -> str\n",
    "print(hex(101010)) # hex -> str\n",
    "print(int('2', 16))  # int 支持字符串\n",
    "print(int(0x23))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'2apple\\xe6\\x88\\x91'\n",
      "[50, 97, 112, 112, 108, 101, 230, 136, 145]\n",
      "b'abc'\n",
      "b'abc'\n",
      "b'abc'\n"
     ]
    }
   ],
   "source": [
    "# 字节串， 字符串\n",
    "print('2apple我'.encode(encoding='utf-8'))        # ascii 码在字节串中直接以本身显示，其他用十六进制显示，一个\\就是一个字节\n",
    "print(list('2apple我'.encode(encoding='utf-8')))  # 显示每个字符的码点\n",
    "# utf-8 latin-1 兼容 ascii\n",
    "print('abc'.encode('utf-8'))\n",
    "print('abc'.encode('latin-1'))\n",
    "print('abc'.encode('ascii'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '进制.ipynb'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m/Users/apple/dsc/dsc/21.编码/进制编码.ipynb Cell 4\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/21.%E7%BC%96%E7%A0%81/%E8%BF%9B%E5%88%B6%E7%BC%96%E7%A0%81.ipynb#W3sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mchardet\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/21.%E7%BC%96%E7%A0%81/%E8%BF%9B%E5%88%B6%E7%BC%96%E7%A0%81.ipynb#W3sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39;49m(\u001b[39m'\u001b[39;49m\u001b[39m进制.ipynb\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mrb\u001b[39;49m\u001b[39m'\u001b[39;49m) \u001b[39mas\u001b[39;00m f:\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/21.%E7%BC%96%E7%A0%81/%E8%BF%9B%E5%88%B6%E7%BC%96%E7%A0%81.ipynb#W3sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m     \u001b[39mprint\u001b[39m(chardet\u001b[39m.\u001b[39mdetect(f\u001b[39m.\u001b[39mread()))\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '进制.ipynb'"
     ]
    }
   ],
   "source": [
    "\n",
    "import chardet\n",
    "with open('进制.ipynb', 'rb') as f:\n",
    "    print(chardet.detect(f.read()))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('opencv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b04e532e6ce11d72ca7f2113c964d7a5531c18c5e6d3e97b1c42c6f9a3724305"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
